package com.handtransaction.service.impl;

import com.handtransaction.dao.UserDao;
import com.handtransaction.entity.User;
import com.handtransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YAO
 * @create 2021-03-24 15:25
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void registUser(User user) {
        userDao.SaveUser(user);
    }

    @Override
    public User loginUser(User user) {
        Map<String, String> result = new HashMap<String, String>();
        result.put("uname", user.getUname());
        result.put("upassword", user.getUpassword());
        return userDao.queryUserByunameAndupassword(result);
    }

    @Override
    public boolean isExistsUsername(String uname) {
        if (userDao.queryUserByuname(uname) == null) {
            return false;
        }
        return true;
    }

    @Override
    public boolean isExistsUserId(Integer uid) {
        if (userDao.queryUserByuid(uid) == null) {
            return false;
        }
        return true;
    }

    @Override
    public User QueryUserByuid(Integer uid) {
        return userDao.queryUserByuid(uid);
    }

    @Override
    public boolean QueryUserByUnumber(Long unumber) {
        if(userDao.queryUserByUnumber(unumber)==null){
            return false;
        }
        return true;
    }

    @Override
    public void UpdateUserInfo(User user) {
        userDao.updateUserInfo(user);
    }

    @Override
    public void UpDGoodsUname(User user) {
        userDao.updateGOODsUname(user);
    }

    @Override
    public void UpdateUserInfo2(User user) {
        userDao.updateUserInfo2(user);
    }

    @Override
    public void updateBlance(User user) {
        userDao.updateUserBalance(user);
    }

    @Override
    public void cutUbanlance(User user) {
        userDao.cutUbanlance(user);
    }

    @Override
    public void addUbalance(User user) {
        userDao.addUbanlance(user);
    }

    @Override
    public List<User> QueryUserByname(String uname) {
        String[] a = uname.split("\\s+");
        uname = "%";
        for (int i = 0; i < a.length; i++) {
            uname = uname + a[i] + "%";
        }
        return userDao.queryUserByname(uname);
    }

    @Override
    public List<User> readAllUser() {
        return userDao.queryAllUser();
    }

    @Override
    public int isfrozenUser(Integer uid,Integer upid) {
        User user = userDao.queryUserByuid(uid);
        if(upid == 1)
            user.setUpid(2);
        if(upid == 2)
            user.setUpid(1);
        return userDao.isfrozenUser(user);
    }

    @Override
    public int deleteUserByAdmin(Integer uid) {
        User user = userDao.queryUserByuid(uid);
        //当点击用户删除后又刷新页面（即重新提交请求，但此时用户已被删除）
        if(user == null){
            return 404;
        }
        if(user.getUpid() == 2){
            //该用户状态为冻结，可删除
            return userDao.deleteUserByAdmin(uid);
        }
        return 0;
    }
}
